package com.dengqi.mymodule.activity.guide;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ViewFlipper;

import com.dengqi.mymodule.BaseActivity;
import com.dengqi.mymodule.R;

import androidx.annotation.Nullable;

/**
 * 适用于自动滑动
 * viewpager与viewflipper的区别：https://blog.csdn.net/xiaoqiang_0719/article/details/51745443
 * 坑一：viewflipper只是自动翻页，viewpager才是手动翻页
 * 坑二：viewflipper没有左右滑动事件，需要自己注册GestureDetector和监听OnGestureListener，重写onFling，根据前后手势的变化判断左翻页还是右翻页
 * 坑三：没有adapter只能一个个addview上去
 * 坑四：没有setOnPageChangeListener只能通过AnimationListener间接实现
 * Created by dell on 2018/6/19
 */

public class ViewFlipperActivity extends BaseActivity {

    ViewFlipper viewFlipper;
    int[] imgs = new int[]{R.mipmap.bg_1, R.mipmap.bg_2, R.mipmap.bg_3};
    //            , R.mipmap.bg_4, R.mipmap.bg_5, R.mipmap.bg_6, R.mipmap.bg_7, R.mipmap.bg_8, R.mipmap.bg_9, R.mipmap.bg_10};
    String[] texts = new String[]{"阿啵呲嘚", "分工会", "尽快路漫漫"};

    ViewFlipper viewFlipper2;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guide_viewflipper);

        viewFlipper = findViewById(R.id.viewFlipper);
        for (int i = 0; i < imgs.length; i++) {          // 添加图片源
            ImageView iv = new ImageView(this);
            iv.setImageResource(imgs[i]);
            iv.setScaleType(ImageView.ScaleType.FIT_XY);
            iv.setPadding(20, 20, 20, 20);
            iv.setTag(i);
            iv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int position = Integer.parseInt(v.getTag().toString());
                    Toast.makeText(ViewFlipperActivity.this, "position:" + position, Toast.LENGTH_SHORT).show();
                }
            });
            ViewGroup.LayoutParams vglp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            viewFlipper.addView(iv, vglp);
        }
        viewFlipper.startFlipping();


        viewFlipper2 = findViewById(R.id.viewFlipper2);
        for (int i = 0; i < texts.length; i++) {          // 添加图片源
            TextView tv = new TextView(this);
            tv.setText(texts[i]);
            ViewGroup.LayoutParams vglp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            viewFlipper2.addView(tv, vglp);
        }
        viewFlipper2.startFlipping();
    }


}
